package com.ak.game.service.impl;

import com.ak.game.entity.PnGameRecord;
import com.ak.game.exception.ServiceException;
import com.ak.game.mapper.PnGameRecordMapper;
import com.ak.game.service.PnGameRecordService;
import com.ak.game.service.PnUserStreamService;
import com.ak.game.util.CommonUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * 自研游戏开奖记录表(PnGameRecord)表服务实现类
 *
 * @author makejava
 * @since 2024-05-20 11:25:00
 */
@Service
public class PnGameRecordServiceImpl extends ServiceImpl<PnGameRecordMapper, PnGameRecord> implements PnGameRecordService {

    @Autowired
    private PnUserStreamService userStreamService;

    /**
     * 保存所有用户本局比赛的结果
     * @param object
     */
    @Override
    public void saveGameRecord(Object object,Integer tallyType) {
        HashMap map = null;
        Date endTime = null;
        List<String> allBet = null;
        PnGameRecord pnGameRecord = null;
        try {
            pnGameRecord = new PnGameRecord();
            map = (HashMap) object;
            endTime = CommonUtil.formatLong2Date(map.get("end_time"));
            pnGameRecord.setGameId(map.get("id").toString());
            pnGameRecord.setRecordId(map.get("round_id").toString());
            pnGameRecord.setStartTime(CommonUtil.formatLong2Date(map.get("start_time")));
            pnGameRecord.setEndTime(endTime);
            pnGameRecord.setBetEndTime(CommonUtil.formatLong2Date(map.get("bet_end_time")));
            pnGameRecord.setAwardMultiple(new BigDecimal(map.get("award_multiple").toString()));
            pnGameRecord.setWin(new BigDecimal(map.get("win").toString()));
            pnGameRecord.setAllBet(JSON.toJSONString(map.get("all_bet")));
            allBet = JSONArray.parseArray(map.get("all_bet").toString()).toJavaList(String.class);
            save(pnGameRecord);
        } catch (Exception e) {
            throw new ServiceException("10005");
        }
        userStreamService.saveUserStream(allBet,map.get("id").toString(),map.get("round_id").toString(),endTime,tallyType);
    }


}

